package com.bkgy.modular.tbdsjdatadictionary.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bkgy.modular.tbdsjdatadictionary.dao.TbDsjDataDictionaryDao;
import com.bkgy.modular.tbdsjdatadictionary.entity.JTbDsjDataDictionary;
import com.bkgy.modular.tbdsjdatadictionary.entity.TbDsjDataDictionary;
import com.bkgy.modular.tbdsjdatadictionary.service.TbDsjDataDictionaryService;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.CodiUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;


/**
 * 数据字典表接口具体实现类
 * @author  生成器生成
 * @date 2022-07-28 12:31:19
 */
@Service
public class TbDsjDataDictionaryServiceImpl implements TbDsjDataDictionaryService {

    @Autowired
    private TbDsjDataDictionaryDao tbDsjDataDictionaryDao;

    /**
     * 添加记录
     * @param data json字符串
     * @param userId 用户id
     * @param sname 用户姓名
     */
    public ResultData insertDataTbDsjDataDictionary(String data, Long userId, String sname){
        try{
            JTbDsjDataDictionary jtbDsjDataDictionary = JSON.parseObject(data,JTbDsjDataDictionary.class);
            TbDsjDataDictionary tbDsjDataDictionary = jtbDsjDataDictionary.getTbDsjDataDictionary();
            CodiUtil.newRecord(userId,sname,tbDsjDataDictionary);
            tbDsjDataDictionaryDao.insertDataTbDsjDataDictionary(tbDsjDataDictionary);
            return ResultData.ResultDataSuccessSelf("添加数据成功", null);
        }catch(Exception e){
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("添加失败，错误信息为" + e.getMessage(), null);
        }
    };

    /**
     * 根据主键删除对象
     * @param
     * */
    public ResultData deleteDataTbDsjDataDictionaryOne(String dic_variable_name,String in_data_source){
        try {
            tbDsjDataDictionaryDao.deleteDataTbDsjDataDictionaryOne(dic_variable_name,in_data_source);
            return ResultData.ResultDataSuccessSelf("删除数据成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("删除失败,失败信息为" + e.getMessage(), null);
        }
    };

    /**
     * 根据主键字符串删除多个对象
     * @param str_id 对象主键字符串
     */
    public ResultData deleteDataTbDsjDataDictionaryMany(String str_id) {
        try {
            String sql = "delete tb_dsj_data_dictionary where indocno in(" + str_id +")";
            tbDsjDataDictionaryDao.deleteDataTbDsjDataDictionaryMany(sql);
            return ResultData.ResultDataSuccessSelf("删除数据数据成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("删除失败,失败信息为" + e.getMessage(), null);
        }
    }

    /**
     * 修改记录
     * @param userId 用户id
     * @param sname 用户姓名
     * @param data sysUser 对象实体
     */
    public ResultData updateDataTbDsjDataDictionary(String data,Long userId,String sname){
        try {
            JTbDsjDataDictionary jtbDsjDataDictionary = JSON.parseObject(data,JTbDsjDataDictionary.class);
            TbDsjDataDictionary tbDsjDataDictionary = jtbDsjDataDictionary.getTbDsjDataDictionary();
            CodiUtil.editRecord(userId,sname,tbDsjDataDictionary);
            tbDsjDataDictionaryDao.updateDataTbDsjDataDictionary(tbDsjDataDictionary);
            return ResultData.ResultDataSuccessSelf("修改成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("修改失败,失败信息为" + e.getMessage(), null);
        }
    };

    /**
     * 分页查看记录
     * @param data 分页参数字符串
     */
    public ResultData findDataTbDsjDataDictionaryByPage(String data) {
        try {
            JTbDsjDataDictionary jtbDsjDataDictionary = JSON.parseObject(data, JTbDsjDataDictionary.class);
            JSONObject jsonObject  = null;
            Integer pageIndex = jtbDsjDataDictionary.getPageIndex();
            Integer pageSize = jtbDsjDataDictionary.getPageSize();

            if(null == pageIndex || null == pageSize){
                return ResultData.ResultDataFaultSelf("分页参数没有传", null);
            }

            if(null!=jtbDsjDataDictionary.getCondition()){
                jsonObject  = JSON.parseObject(jtbDsjDataDictionary.getCondition().toString());
            }
            String dic_variable_description = null;
            if (!StringUtils.isEmpty(jsonObject.get("dic_variable_description"))) {
                dic_variable_description = jsonObject.get("dic_variable_description").toString();
            }

            String production_line = null;
            if (!StringUtils.isEmpty(jsonObject.get("production_line"))) {
                production_line = jsonObject.get("production_line").toString();
            }
            String dic_variable_name = null;
            if (!StringUtils.isEmpty(jsonObject.get("dic_variable_name"))) {
                dic_variable_name = jsonObject.get("dic_variable_name").toString();
            }


            List<TbDsjDataDictionary> list = tbDsjDataDictionaryDao.findDataTbDsjDataDictionaryByPage((pageIndex-1)*pageSize, pageIndex* pageSize,dic_variable_description,production_line,dic_variable_name);
            Integer count = tbDsjDataDictionaryDao.findDataTbDsjDataDictionaryByPageSize(dic_variable_description,production_line,dic_variable_name);
            return ResultData.ResultDataSuccess(list, count);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null);
        }
    }

    /**
     * 根据主键查询单条记录
     * @param data 分页参数字符串
     */
    public ResultData findDataTbDsjDataDictionaryByIndocno(String data) {
        try {
            JTbDsjDataDictionary jtbDsjDataDictionary = JSON.parseObject(data, JTbDsjDataDictionary.class);
            Long indocno = jtbDsjDataDictionary.getIndocno();

            TbDsjDataDictionary tbDsjDataDictionary = tbDsjDataDictionaryDao.findDataTbDsjDataDictionaryByIndocno(indocno);
            return ResultData.ResultDataSuccess(tbDsjDataDictionary);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null);
        }
    }

    /**
     * 查看记录
     * @return list 对象集合返回
     * */
    public List<TbDsjDataDictionary> findDataTbDsjDataDictionary(){
        List<TbDsjDataDictionary> list = tbDsjDataDictionaryDao.findDataTbDsjDataDictionary();
        return list;
    };
}
